<?php
declare(strict_types=1);

namespace app\utils;


class ArrayUtil
{

    public static function getTree($arr, $pid, $step, $tree = [])
    {
        foreach ($arr as $key => $val) {
            if ($val['parent_id'] == $pid) {
                $val['child'] = [];
                $tree[] = $val;
                self::GetTree($arr, $val['id'], $step + 1,$tree);
            }
        }
        return empty($tree) ? [] : $tree;
    }

    public static function getTreeNoChild($arr, $pid, $step, $tree = [])
    {
        foreach ($arr as $key => $val) {
            if ($val['parent_id'] == $pid) {
                $tree[] = $val;
                if ($arr = self::GetTree($arr, $val['id'], $step + 1,$tree)) {
                    $val['child'] = $arr;
                }
            }
        }
        return empty($tree) ? [] : $tree;
    }
}